<template>
  <el-dialog
    title="分配角色"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="50%"
    :before-close="handleClose"
  >
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <div slot="footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="onConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { roleListApi } from '@/api/setting'
import { getProfileById } from '@/api/user'
import { assignRolesApi } from '@/api/employees'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      checkList: [], // 选中的角色id
      roleList: [], // 存放角色列表
      userId: '' // 用户id
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    // 请求用户信息接口，获取该用户角色列表回显
    async getUserInfo(id) {
      this.userId = id
      const { roleIds } = await getProfileById(id)
      if (!roleIds) return
      this.checkList = roleIds
    },
    // 确认分配角色
    async onConfirm() {
      await assignRolesApi({ id: this.userId, roleIds: this.checkList })
      this.handleClose()
      this.$message.success('操作成功')
    }, // 获取角色列表
    async getRoles() {
      const { rows } = await roleListApi({
        page: 1,
        pagesize: 10000
      })
      this.roleList = rows
    },
    // 处理弹框关闭
    handleClose() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style>
</style>